package org.eclipse.rally.plugin.utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CalenderChooser {
	public static Shell shell;
	private static Label open;
	private static Text text;
	public static void main (String [] args) {
		Display display = new Display ();
		shell = new Shell (display);
		shell.setMinimumSize(new Point(400, 100));
		shell.setLayout(new FillLayout());
		Composite composite = new Composite(shell, SWT.NONE);
		Group group = new Group(composite, SWT.NONE);
		group.setBounds(0, 10, 382, 146);
	//	DateChooserChombo cc=new DateC
				open= new Label (group, SWT.PUSH);
				open.setBounds(272, 10, 70, 19);
				open.setText ("Open Dialog");
				{
					text = new Text(group, SWT.BORDER);
					text.setBounds(106, 10, 160, 19);
				}
		DatePicker(shell,text,open);
		shell.pack ();
		shell.open ();
		
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}
	public static void DatePicker(Shell shell_1, Text text_1,Label open_1)
	{
		shell=shell_1;
		text=text_1;
		open=open_1;
		final Shell dialog = new Shell (shell, SWT.DIALOG_TRIM);
		dialog.setLayout (new GridLayout (1, true));
		 open.setEnabled(true);
		open.addMouseListener(new MouseAdapter() {
	          @Override
	          public void mouseDown(MouseEvent e) {
	          // CalenderChooser.DatePicker(shell, text_1, open_1);
	        	  open.setEnabled(false);
				/*open.addSelectionListener (new SelectionAdapter () {
					public void widgetSelected (SelectionEvent e) {*/
						

						final DateTime calendar = new DateTime (dialog, SWT.CALENDAR | SWT.BORDER);
						final DateTime date = new DateTime (dialog, SWT.DATE | SWT.SHORT);
						final DateTime time = new DateTime (dialog, SWT.TIME | SWT.SHORT);
						date.setVisible(false);
						time.setVisible(false);
						calendar.addListener(SWT.MouseDoubleClick, new Listener() {
				        	 public void handleEvent(Event e) {
				        		 
				        		 text.setText(""+ (calendar.getMonth () + 1) + "/" + calendar.getDay () + "/" + calendar.getYear ());
				        		 dialog.close ();
				        		
				        	 }
						});
					
						dialog.pack ();
						dialog.open ();
						
					}
	          
				});
		 open.setEnabled(true);
	}
	} 
